import net.spy.memcached.CASResponse;
import net.spy.memcached.CASValue;
import net.spy.memcached.MemcachedClient;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

/**
 * Created by LiuChang on 2017/4/1.
 */
public class MemcachedJava {
    public static void main(String[] args) {
        MemcachedJava memcachedJava = new MemcachedJava();
        try {
            // 本地连接 Memcached 服务
            MemcachedClient memcachedClient = new MemcachedClient(new InetSocketAddress("127.0.0.1", 11211));
            System.out.println("Connection to server sucessful.");

//            memcachedJava.set(memcachedClient);
//            memcachedJava.add(memcachedClient);
//            memcachedJava.replace(memcachedClient);
//            memcachedJava.append(memcachedClient);
//            memcachedJava.prepend(memcachedClient);
//            memcachedJava.cas(memcachedClient);
//            memcachedJava.get(memcachedClient);
            memcachedJava.gets(memcachedClient);

            //关闭连接
            memcachedClient.shutdown();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
    }

    public void set(MemcachedClient memcachedClient) throws ExecutionException, InterruptedException {
        //存储数据
        Future fo = memcachedClient.set("runoob", 900, "Free Education");
        //查看存储状态
        System.out.println("set status：" + fo.get());
        //输出值
        System.out.println("runoob value in cache - " + memcachedClient.get("runoob"));
    }

    public void add(MemcachedClient memcachedClient) throws ExecutionException, InterruptedException {
        Future fo = memcachedClient.set("runoob", 900, "Free Education");
        System.out.println("set status：" + fo.get());
        System.out.println("runoob value in cache - " + memcachedClient.get("runoob"));
        memcachedClient.add("runoob", 900, "memcached");
        System.out.println("add status：" + fo.get());
        // 添加新key
        fo = memcachedClient.add("codingground", 900, "All Free Compilers");

        // 打印状态
        System.out.println("add status:" + fo.get());

        // 输出
        System.out.println("codingground value in cache - " + memcachedClient.get("codingground"));
    }

    public void replace(MemcachedClient memcachedClient) throws ExecutionException, InterruptedException {
        // 添加第一个 key=》value 对
        Future fo = memcachedClient.set("runoob", 900, "Free Education");

        // 输出执行 add 方法后的状态
        System.out.println("add status:" + fo.get());

        // 获取键对应的值
        System.out.println("runoob value in cache - " + memcachedClient.get("runoob"));

        // 添加新的 key
        fo = memcachedClient.replace("runoob", 900, "Largest Tutorials' Library");

        // 输出执行 set 方法后的状态
        System.out.println("replace status:" + fo.get());

        // 获取键对应的值
        System.out.println("runoob value in cache - " + memcachedClient.get("runoob"));

    }

    public void append(MemcachedClient memcachedClient) throws ExecutionException, InterruptedException {
        // 添加数据
        Future fo = memcachedClient.set("runoob", 900, "Free Education");

        // 输出执行 set 方法后的状态
        System.out.println("set status:" + fo.get());

        // 获取键对应的值
        System.out.println("runoob value in cache - " + memcachedClient.get("runoob"));

        // 对存在的key进行数据添加操作
        fo = memcachedClient.append("runoob", " for All");

        // 输出执行 set 方法后的状态
        System.out.println("append status:" + fo.get());

        // 获取键对应的值
        System.out.println("runoob value in cache - " + memcachedClient.get("runoob"));

    }

    public void prepend(MemcachedClient memcachedClient) throws ExecutionException, InterruptedException {
        // 添加数据
        Future fo = memcachedClient.set("runoob", 900, "Education for All");

        // 输出执行 set 方法后的状态
        System.out.println("set status:" + fo.get());

        // 获取键对应的值
        System.out.println("runoob value in cache - " + memcachedClient.get("runoob"));

        // 对存在的key进行数据添加操作
        fo = memcachedClient.prepend("runoob", "Free ");

        // 输出执行 set 方法后的状态
        System.out.println("prepend status:" + fo.get());

        // 获取键对应的值
        System.out.println("runoob value in cache - " + memcachedClient.get("runoob"));
    }

    public void cas(MemcachedClient memcachedClient) throws ExecutionException, InterruptedException {
        // 添加数据
        Future fo = memcachedClient.set("runoob", 900, "Free Education");

        // 输出执行 set 方法后的状态
        System.out.println("set status:" + fo.get());

        // 使用 get 方法获取数据
        System.out.println("runoob value in cache - " + memcachedClient.get("runoob"));

        // 通过 gets 方法获取 CAS token（令牌）
        CASValue casValue = memcachedClient.gets("runoob");

        // 输出 CAS token（令牌） 值
        System.out.println("CAS token - " + casValue);

        // 尝试使用cas方法来更新数据
        CASResponse casresp = memcachedClient.cas("runoob", casValue.getCas(), 900, "Largest Tutorials-Library");

        // 输出 CAS 响应信息
        System.out.println("CAS Response - " + casresp);

        // 输出值
        System.out.println("runoob value in cache - " + memcachedClient.get("runoob"));
    }

    public void get(MemcachedClient memcachedClient) throws ExecutionException, InterruptedException {
        // 添加数据
        Future fo = memcachedClient.set("runoob", 900, "Free Education");

        // 输出执行 set 方法后的状态
        System.out.println("set status:" + fo.get());

        // 使用 get 方法获取数据
        System.out.println("runoob value in cache - " + memcachedClient.get("runoob"));

    }

    public void gets(MemcachedClient memcachedClient) throws ExecutionException, InterruptedException {
        // 添加数据
        Future fo = memcachedClient.set("runoob", 900, "Free Education");

        // 输出执行 set 方法后的状态
        System.out.println("set status:" + fo.get());

        // 从缓存中获取键为 runoob 的值
        System.out.println("runoob value in cache - " + memcachedClient.get("runoob"));

        // 通过 gets 方法获取 CAS token（令牌）
        CASValue casValue = memcachedClient.gets("runoob");

        // 输出 CAS token（令牌） 值
        System.out.println("CAS value in cache - " + casValue);

    }

}
